(ns restfulclojure
 (:require
  [compojure.core :refer [defroutes GET]]
  [ring.adapter.jetty :as jetty]
  clojure.java.shell))

;; View functions

(defn view [x]
 (str "<h1>" x "</h1>"))

(defn index []
 (view "Hello, please input url like http://ip:3000/mem/  or 
									 http://ip:3000/disk/ or
									 http://ip:3000/cpu/"))

(defn exec [[arg1 arg2]]
 (try
        (let [{:keys [exit out err]} (clojure.java.shell/sh arg1 arg2)]
        (if (= exit 0)
                                (view out)
                                (view err)
        )
        )
(catch Exception e (do (view "Get info from local Machine cause by error")))))


;; Routing

(defroutes main-routes
 (GET "/" [] (index))
 (GET "/mem/" [] (exec ["free" "-m"]))
 (GET "/disk/" [] (exec ["df" "-k"]))
 (GET "/cpu/" [] (exec ["cat" "/proc/stat"]))
)


;; Server

(defn -main []
 (jetty/run-jetty main-routes {:port 3000}))

